package com.systemdemo;

public class SystemDemo2 {
    public static void main(String[] args) {

        //判断1~100000之间有多少个素数
        long start1 = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            boolean flag = isPrime1(i);
            if (flag){
                System.out.println(i);
            }
        }
        long end1 = System.currentTimeMillis();
        System.out.println("================");


        long start2 = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            boolean flag = isPrime2(i);
            if (flag){
                System.out.println(i);
            }
        }
        long end2 = System.currentTimeMillis();
        System.out.println("================");
        System.out.println(end1-start1);
        System.out.println(end2-start2);
    }

    //以前的方法
    public static boolean isPrime1(int number){
        for (int i = 2; i < number; i++) {
            if(number%i==0){
                return false;
            }
        }
        return true;
    }
    //改进后的方法
    public static boolean isPrime2(int number){
        for (int i = 2; i <=Math.sqrt(number) ; i++) {
            if(number%i==0){
                return false;
            }
        }
        return true;
    }
}
